/**
 *
 */
package net.haroeris.wh40kcampaign.service.impl;

import java.util.Collection;
import java.util.LinkedList;

import net.haroeris.wh40kcampaign.beans.po.Campaign;
import net.haroeris.wh40kcampaign.beans.to.CampaignTO;
import net.haroeris.wh40kcampaign.dao.CampaignDao;
import net.haroeris.wh40kcampaign.service.CampaignService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author user
 *
 */
@Service("campaignService")
public class CampaignServiceImpl implements CampaignService {
	@Autowired
	private CampaignDao campaignDao;

	/* (non-Javadoc)
	 * @see net.haroeris.wh40kcampaign.service.CampaignService#getAllCampaigns()
	 */
	@Transactional(readOnly=true)
	public Collection<CampaignTO> getAllCampaigns() {
		Collection<CampaignTO> allCampaigns= new LinkedList<CampaignTO>();

		for( Campaign campaign : campaignDao.getAllCampaigns() ){
			allCampaigns.add(createCampaignTO(campaign));
		}
		return allCampaigns;
	}

	/* (non-Javadoc)
	 * @see net.haroeris.wh40kcampaign.service.CampaignService#getCampaign()
	 */

	//    @Transactional(readOnly=true)
	public CampaignTO getCampaign( Long id) {
		Campaign campaign= campaignDao.getCampaign(id);
		if( campaign!=null ){
			return createCampaignTO(campaign);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see net.haroeris.wh40kcampaign.service.CampaignService#createCampaign(net.haroeris.wh40kcampaign.beans.po.Campaign)
	 */
	@Transactional
	public boolean saveCampaign( CampaignTO campaign ){
		Campaign thisCampaign= null;

		if( campaign.getId()!=null ){
			thisCampaign= campaignDao.getCampaign(campaign.getId());
		}

		boolean isNew= thisCampaign==null;

		if( isNew ){
			campaignDao.createCampaign(createNewCampaign(campaign));
		} else {
			mapExistingCampaign(campaign,thisCampaign);
			campaignDao.saveCampaign(thisCampaign);
		}

		return true;
	}

	/* (non-Javadoc)
	 * @see net.haroeris.wh40kcampaign.service.CampaignService#deleteCampaign(net.haroeris.wh40kcampaign.beans.po.Campaign)
	 */
	@Transactional
	public boolean deleteCampaign(CampaignTO campaign) {
		return campaignDao.delete( campaign.getId() );
	}

	/* (non-Javadoc)
	 * @see net.haroeris.wh40kcampaign.service.CampaignService#deleteCampaign(java.lang.Long)
	 */
	@Transactional
	public boolean deleteCampaign(Long id) {
		return campaignDao.delete( id );
	}
	/*
    @Override
    public Collection<PlayerTO> getPlayerForCampaign(Campaign campaign) {
        return getPlayerForCampaign(campaign.getId());
    }

    @Override
    public Collection<PlayerTO> getPlayerForCampaign(Long campaignID) {

        Collection<PlayerTO> campaignPlayers= new HashSet<PlayerTO>();

        Collection<Army> campaignArmies= getArmiesForCampaign(campaignID);

        return campaignPlayers;
    }


    public Collection<Army> getArmiesForCampaign( Campaign campaign ){
        return getArmiesForCampaign(campaign.getId());
    }

    public Collection<Army> getArmiesForCampaign( Long campaignID ){
        Campaign campaign= getCampaign(campaignID);

        Collection<Army> campaignArmies= new HashSet<Army>();

        if( campaign!=null ){
            campaign.getParties();
        }

        return campaignArmies;
    }
	 */

	private void mapExistingCampaign(CampaignTO campaign, Campaign thisCampaign) {
		if( campaign==null ){
			throw new IllegalArgumentException("campaignTO must not be NULL!");
		}
		if( thisCampaign==null ){
			throw new IllegalArgumentException("campaign must not be NULL!");
		}
		if( thisCampaign.getId()!=campaign.getId() ){
			throw new IllegalArgumentException("campaign and campaignTO must have the same ID!");
		}
		thisCampaign.setDescription(campaign.getDescription());
		thisCampaign.setName(campaign.getName());
	}

	private Campaign createNewCampaign(CampaignTO campaign) {
		Campaign newCampaign= new Campaign();

		newCampaign.setDescription(campaign.getDescription());
		newCampaign.setName(campaign.getName());

		return newCampaign;
	}

	private CampaignTO createCampaignTO(Campaign campaign) {
		CampaignTO newCampaign= new CampaignTO();

		newCampaign.setId(campaign.getId());
		newCampaign.setDescription(campaign.getDescription());
		newCampaign.setName(campaign.getName());

		return newCampaign;
	}
}